package com.whty.module.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.whty.common.annotation.Log;
import com.whty.common.base.AjaxResult;
import com.whty.common.config.Global;
import com.whty.common.enums.BusinessType;
import com.whty.framework.util.FileUploadUtils;
import com.whty.framework.web.page.TableDataInfo;
import com.whty.system.domain.ProductDetail;
import com.whty.system.service.IProductDetailService;
import com.whty.web.core.base.BaseController;

/**
 * 溯源详情 信息操作处理
 * 
 * @author mario
 * @date 2018-10-31
 */
@Controller
@RequestMapping("/module/productDetail")
public class ProductDetailController extends BaseController
{
    private String prefix = "module/productDetail";
    
    private static Logger logger = LoggerFactory.getLogger(ProductDetailController.class);
	
	@Autowired
	private IProductDetailService productDetailService;
	
	@RequiresPermissions("module:productDetail:view")
	@GetMapping()
	public String productDetail()
	{
	    return prefix + "/productDetail";
	}
	
	/**
	 * 查询溯源详情列表
	 */
	@RequiresPermissions("module:productDetail:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ProductDetail productDetail)
	{
		startPage();
        List<ProductDetail> list = productDetailService.selectProductDetailList(productDetail);
		return getDataTable(list);
	}
	
	/**
	 * 新增溯源详情
	 */
//	@GetMapping("/add/{dictType}")
	@GetMapping("/add/{productCode}")
	public String add(@PathVariable("productCode") String productCode, ModelMap mmap)

	{   
		logger.info("传入的溯源码为："+productCode);
		mmap.put("productCode", productCode);
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存溯源详情
	 */
	@RequiresPermissions("module:productDetail:add")
	@Log(title = "溯源详情", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(HttpServletRequest request)
	{		
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
         Map<String, MultipartFile> files = multipartRequest.getFileMap();
    	 MultipartFile image1 =files.get("file1");
    	 MultipartFile image2 =files.get("file2");
    	 MultipartFile image3 =files.get("file3");
    	 MultipartFile image4 =files.get("file4");
    	 MultipartFile image5 =files.get("file5");
		 MultipartFile image = files.get("files[]");
		 String fileStr1 = "";
		 String fileStr2 = "";
		 String fileStr3 = "";
		 String fileStr4 = "";
		 String fileStr5 = "";
		 ProductDetail productDetail =new ProductDetail();
		 if(productDetail==null){
			 return AjaxResult.error("修改溯源信息失败"); 
		 }
		 try{
			 if (!image1.isEmpty()){
				  fileStr1 = FileUploadUtils.upload(Global.getAvatarPath(), image1); 
				  productDetail.setImage1(fileStr1);
				 
			 }if (!image2.isEmpty()){
				  fileStr2 = FileUploadUtils.upload(Global.getAvatarPath(), image2); 
				  productDetail.setImage2(fileStr2);
				 
			 }if (!image3.isEmpty()){
				  fileStr3 = FileUploadUtils.upload(Global.getAvatarPath(), image3); 
				  productDetail.setImage3(fileStr3);
			 }if (!image4.isEmpty()){
				  fileStr4 = FileUploadUtils.upload(Global.getAvatarPath(), image4); 
				  productDetail.setImage4(fileStr4);
			 }if (!image5.isEmpty()){
				  fileStr5 = FileUploadUtils.upload(Global.getAvatarPath(), image5); 
				  productDetail.setImage5(fileStr5);
			 }  
				String productCode= request.getParameter("productCode");
				productDetail.setProductCode(productCode);
				String descInfo= request.getParameter("descInfo");
				productDetail.setDescInfo(descInfo);
				String processType= request.getParameter("processType");
				if(processType!=null&&!processType.equals("")){
					productDetail.setProcessType(Integer.parseInt(processType));	
				}
				
				String opeUser= request.getParameter("opeUser");
				productDetail.setOpeUser(opeUser);
			
//				String opeTime= request.getParameter("opeTime");
//				 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//				 if(opeTime!=null&&!opeTime.equals("")){
//						productDetail.setOpeTime(sdf.parse(opeTime)); 
//				 }
				productDetail.setOpeTime(new Date());
				String remarks= request.getParameter("remarks");
				productDetail.setRemark(remarks);
				return toAjax(productDetailService.insertProductDetail(productDetail));
		 }catch(Exception e){
			 logger.info("修改溯源信息失败", e);
			 return AjaxResult.error("修改溯源信息失败");
		 }

	}

	/**
	 * 修改溯源详情
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		ProductDetail productDetail = productDetailService.selectProductDetailById(id);
		mmap.put("productDetail", productDetail);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存溯源详情
	 */
	@RequiresPermissions("module:productDetail:edit")
	@Log(title = "溯源详情", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(HttpServletRequest request)
	{		
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
         Map<String, MultipartFile> files = multipartRequest.getFileMap();
    	 MultipartFile image1 =files.get("file1");
    	 MultipartFile image2 =files.get("file2");
    	 MultipartFile image3 =files.get("file3");
    	 MultipartFile image4 =files.get("file4");
    	 MultipartFile image5 =files.get("file5");
		 MultipartFile image = files.get("files[]");
		 String fileStr1 = "";
		 String fileStr2 = "";
		 String fileStr3 = "";
		 String fileStr4 = "";
		 String fileStr5 = "";

		 
		 String id=request.getParameter("id");
		 ProductDetail productDetail = productDetailService.selectProductDetailById(Long.parseLong(id));
		 try{
			 if (!image1.isEmpty()){
				  fileStr1 = FileUploadUtils.upload(Global.getAvatarPath(), image1); 
				  productDetail.setImage1(fileStr1);
				 
			 }if (!image2.isEmpty()){
				  fileStr2 = FileUploadUtils.upload(Global.getAvatarPath(), image2); 
				  productDetail.setImage2(fileStr2);
				 
			 }if (!image3.isEmpty()){
				  fileStr3 = FileUploadUtils.upload(Global.getAvatarPath(), image3); 
				  productDetail.setImage3(fileStr3);
			 }if (!image4.isEmpty()){
				  fileStr4 = FileUploadUtils.upload(Global.getAvatarPath(), image4); 
				  productDetail.setImage4(fileStr4);
			 }if (!image5.isEmpty()){
				  fileStr5 = FileUploadUtils.upload(Global.getAvatarPath(), image5); 
				  productDetail.setImage5(fileStr5);
			 }  
				String productCode= request.getParameter("productCode");
				productDetail.setProductCode(productCode);
				String descInfo= request.getParameter("descInfo");
				productDetail.setDescInfo(descInfo);
				String processType= request.getParameter("processType");
				if(processType!=null&&!processType.equals("")){
					productDetail.setProcessType(Integer.parseInt(processType));	
				}
				
				String opeUser= request.getParameter("opeUser");
				productDetail.setOpeUser(opeUser);
			
				String opeTime= request.getParameter("opeTime");
				 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				 if(opeTime!=null&&!opeTime.equals("")){
						productDetail.setOpeTime(sdf.parse(opeTime)); 
				 }
			
				String remarks= request.getParameter("remarks");
				productDetail.setRemark(remarks);
				return toAjax(productDetailService.updateProductDetail(productDetail));
		 }catch(Exception e){
			 logger.info("上传溯源信息失败", e);
			 return AjaxResult.error("上传溯源信息失败");
		 }

	}
	
	/**
	 * 删除溯源详情
	 */
	@RequiresPermissions("module:productDetail:remove")
	@Log(title = "溯源详情", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(productDetailService.deleteProductDetailByIds(ids));
	}
	
	/**
	 * 检查流程类型
	 */
	@PostMapping("/checkProcessType")
	@ResponseBody
	public Integer checkProcessType(String productCode,Integer processType){
		int f = 0;
		ProductDetail productDetail = new ProductDetail();
		productDetail.setProductCode(productCode);
		productDetail.setProcessType(processType);
		List<ProductDetail> productDetailList = productDetailService.selectProductDetailList(productDetail);
		if(productDetailList != null && productDetailList.size() > 0){
			f = 1;
		}
		return f;
	}
}
